package googleTranslateTestSwing;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import com.google.api.translate.Language;

public class LanguageMouseListener extends MouseAdapter {

    /**
     * The listener interface for receiving languageMouse events. The class that
     * is interested in processing a languageMouse event implements this
     * interface, and the object created with that class is registered with a
     * component using the component's <code>addLanguageMouseListener</code>
     * method. When the languageMouse event occurs, that object's appropriate
     * method is invoked.
     * @see LanguageMouseEvent
     */

    private final JComponent comp;
    private final String text;
    private Language from;
    private Language to;

    private LanguageMouseListener(JComponent comp, String text) {
        this.comp = comp;
        this.text = text;
        comp.addMouseListener(this);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        super.mouseEntered(e);
        comp.setCursor(new Cursor(Cursor.HAND_CURSOR));
    }

    @Override
    public void mouseExited(MouseEvent e) {
        super.mouseExited(e);
        comp.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        if (e.getClickCount() == 2) {
            try {
                String translatedText = doTranslate(comp, text);
                System.err.println(translatedText);
            } catch (Exception e1) {
                System.err.println(e1.getMessage());
            }
        }
    }

    public void activateLanguageChange(JComponent component, Language from, Language to) {
        this.from = from;
        this.to = to;
        translate(component);
    }

    private void translate(JComponent component) {
        for (Component c : component.getComponents()) {
            if (c instanceof JComponent) {
                JComponent subComp = (JComponent) c;
                if (subComp.getComponentCount() > 0) {
                    translate(subComp);
                }
                // getText from components and translate
                if (c instanceof JLabel && !(c.getParent().getParent() instanceof JTabbedPane)) {
                    JLabel lbl = (JLabel) c;
                    doTranslate(lbl, lbl.getText());
                    addMouseOverListenerToComponent(subComp, ((JLabel) c).getText());
                }
                if (c instanceof JCheckBox) {
                    JCheckBox lbl = (JCheckBox) c;
                    doTranslate(lbl, lbl.getText());
                    addMouseOverListenerToComponent(subComp, ((JCheckBox) c).getText());
                }
                if (c instanceof JRadioButton) {
                    JRadioButton lbl = (JRadioButton) c;
                    doTranslate(lbl, lbl.getText());
                    addMouseOverListenerToComponent(subComp, ((JRadioButton) c).getText());
                }
                if (c instanceof JButton) {
                    JButton lbl = (JButton) c;
                    doTranslate(lbl, lbl.getText());
                    addMouseOverListenerToComponent(subComp, ((JButton) c).getText());
                }
                if (c instanceof JTextField) {
                    JTextField jtf = (JTextField) c;
                    String text = jtf.getText();
                    if (!jtf.isEditable() && !text.trim().isEmpty()) {
                        doTranslate(jtf, text);
                        addMouseOverListenerToComponent(subComp, text);
                    }
                }
            }
        }

    }

    private String doTranslate(JComponent lbl, String text2) {
        return GoogleTranslator.translate(lbl, text2);
    }

    private static void addMouseOverListenerToComponent(final JComponent comp, final String text) {
        if (comp.getMouseListeners().length > 0) {
            for (MouseListener ml : comp.getMouseListeners())
                comp.removeMouseListener(ml);
        }
        comp.addMouseListener(new LanguageMouseListener(comp, text));
    }
}
